package ev.selection;//need to add this line

import java.util.*;

import ev.initial.Individual;

public class ElitismSelection implements Selection{

	public ArrayList<Individual> select(ArrayList<Individual> oldPopulation, int limit)
	{
		Random r=new Random();
		int size=oldPopulation.size();
		if(size<limit){System.out.println("error: limit can not be larger than population size");}
		Collections.sort(oldPopulation);
		ArrayList<Individual> newPopulation=new ArrayList<Individual>();
		for(int i=0; i<limit-1; i++){
			newPopulation.add(oldPopulation.get(i));
		}
		if(size>limit){			
			int index=r.nextInt(size-limit)+limit-1;
			newPopulation.add(oldPopulation.get(index));
			
		}
		return newPopulation;
	}
}
